package com.yskj.mwk.utils;

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class Convert2Pdf {

    /**
     * 将多张图片转换为 PDF 文件，每张图片占据一页，并预留前两页为空白页。
     * @param sources 图片路径列表
     * @param target 生成的 PDF 文件路径
     */
    public String convert(List<String> sources, String target) {
        Document document = new Document();
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(target);
            PdfWriter writer = PdfWriter.getInstance(document, fos);

            // 设置页面事件
            writer.setPageEvent(new PdfPageEventHelper() {
                @Override
                public void onStartPage(PdfWriter writer, Document document) {
                    document.setMargins(0, 0, 0, 0);
                }
            });

            document.open();

            // 添加图片到 PDF
            for (String source : sources) {
                Image image = Image.getInstance(source);
                float imageWidth = image.getWidth();
                float imageHeight = image.getHeight();

                document.setPageSize(new Rectangle(imageWidth, imageHeight));
                document.newPage();
                image.setAbsolutePosition(0, 0); // 左上角对齐
                image.setBorderWidth(0);
                document.add(image);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return "";
        } finally {
            document.close();
            try {
                if (fos != null) {
                    fos.flush();
                    fos.close();
                }
                // 新增代码：获取文件大小
                File targetFile = new File(target);
                if (targetFile.exists()) {
                    long fileSizeBytes = targetFile.length();
                    double fileSizeKB = fileSizeBytes / 1024.0;
                    double fileSizeMB = fileSizeKB / 1024.0;
                    String formattedMB = String.format("%.2f MB", fileSizeMB);
                    System.out.printf("生成文件大小: %.2f KB (约 %.2f MB)%n", fileSizeKB, fileSizeMB);
                    return formattedMB;
                }
            } catch (IOException e) {
                e.printStackTrace();
                return "";
            }
        }
        return "";
    }

    public static void main(String[] args) {
        // 使用 Arrays.asList 创建一个不可变列表
        List<String> sources = Arrays.asList(
                "E:\\ideaWorkSpace\\mwk_admin\\upload\\image\\avatar\\2025\\02\\14/pdf_z.png",
                "E:\\ideaWorkSpace\\mwk_admin\\upload\\image\\avatar\\2025\\02\\14/pdf_f.png",
                "E:\\ideaWorkSpace\\mwk_admin\\upload\\image\\avatar\\2025\\02\\14/7fe87667-e06d-4d11-a171-8d7cd30ccaa1.png",
                "E:\\ideaWorkSpace\\mwk_admin\\upload\\image\\avatar\\2025\\02\\14/1739520647781.png",
                "E:\\ideaWorkSpace\\mwk_admin\\upload\\image\\avatar\\2025\\02\\14/1739521389050.png"
        );
        String target = "./upload/pdfs/out.pdf";
        Convert2Pdf convert2Pdf = new Convert2Pdf();
        convert2Pdf.convert(sources, target);
    }
}